package drds.binlog.parse.mysql.table_meta_data.dao;

import drds.binlog.common.Authors;
import drds.common.Author;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Statement;

@Slf4j
@Author(name = Authors.LI_YANG)
@SuppressWarnings("deprecation")
public class MetaBaseDAO
{


    protected void initTable(String tableName) throws Exception
    {
        Connection connection = null;
        InputStream inputStream = null;
        try
        {
            DataSource dataSource = null;
            connection = dataSource.getConnection();
            String name = "mysql";

            inputStream = Thread.currentThread()
                    .getContextClassLoader()
                    .getResourceAsStream("ddl/" + name + "/" + tableName + ".sql");
            if (inputStream == null)
            {
                return;
            }

            String sql = StringUtils.join(IOUtils.readLines(inputStream), "\n");
            Statement statement = connection.createStatement();
            statement.execute(sql);
            statement.close();
        } catch (Throwable e)
        {
            log.warn("unsettled " + tableName + " failed", e);
        } finally
        {
            IOUtils.closeQuietly(inputStream);
            if (connection != null)
            {
                connection.close();
            }
        }
    }


}
